package com.example.mediademo;

import android.graphics.SurfaceTexture;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Environment;
import android.view.Surface;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mediademo.decode.AudioDecoder;
import com.example.mediademo.decode.VideoDecoder;
import com.example.mediademo.opengl.IDrawder;
import com.example.mediademo.opengl.SimpleRender;
import com.example.mediademo.opengl.SurfaceTextureCallback;
import com.example.mediademo.opengl.VideoDrawer;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class OpenGLPlayerActivity extends AppCompatActivity {

    private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mvtest.mp4";
    private IDrawder mDrawer;
    private GLSurfaceView glSurfaceView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opengl_player);
        glSurfaceView = findViewById(R.id.gl_surface);
        initRender();
    }

    private void initRender() {
        mDrawer = new VideoDrawer(false);
        mDrawer.setVideoSize(544,960);
        mDrawer.getSurfaceTexture(new SurfaceTextureCallback() {
            @Override
            public void surfaceTextureCreate(SurfaceTexture surfaceTexture) {
                initPlayer(new Surface(surfaceTexture));
            }
        });
        glSurfaceView.setEGLContextClientVersion(2);
        glSurfaceView.setRenderer(new SimpleRender(mDrawer));
    }

    private void initPlayer(Surface surface) {
        ExecutorService threadPool = new ThreadPoolExecutor(2,20,60, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
        VideoDecoder videeoDecoder = new VideoDecoder(path,null,surface);
        threadPool.execute(videeoDecoder);

        AudioDecoder audioDecoder = new AudioDecoder(path);
        threadPool.execute(audioDecoder);

        videeoDecoder.resume();
        audioDecoder.resume();
    }
}
